package com.example.hadoop;

import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

/**
 * 接下来，我们编写一个简单的MapReduce程序，用于统计文本文件中每个单词出现的次数。
 * 这个例子会分为三部分：Mapper、Reducer和驱动程序（Driver）。
 */
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        for (String token : line.split("\\s+")) {
            word.set(token);
            context.write(word, one);
        }
    }
}